package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.ILegalCaseContractService;
import com.mdd.admin.validate.LegalCaseContractCreateValidate;
import com.mdd.admin.validate.LegalCaseContractSearchValidate;
import com.mdd.admin.validate.LegalCaseContractUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalCaseContractDetailVo;
import com.mdd.admin.vo.LegalCaseContractListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalCaseContract;
import com.mdd.common.mapper.LegalCaseContractMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 案件合同记录实现类
 * @author LikeAdmin
 */
@Service
public class LegalCaseContractServiceImpl implements ILegalCaseContractService {
        
    @Resource
    LegalCaseContractMapper legalCaseContractMapper;

    /**
     * 案件合同记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalCaseContractListedVo>
     */
    @Override
    public PageResult<LegalCaseContractListedVo> list(PageValidate pageValidate, LegalCaseContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        MPJQueryWrapper<LegalCaseContract> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.selectAll(LegalCaseContract.class)
                .select("t2.area_name as areaName, " +
                        "t3.case_name as caseName, t3.case_trade_no as caseNo, " +
                        "t4.template_name as templateName")
                .leftJoin("legal_case t3 on t3.id = t.case_id")
                .leftJoin("legal_area_city t2 on t2.area_id = t3.area_id")
                .leftJoin("la_mediation_template t4 on t4.template_number = t.template_id")
                .orderByDesc("t.id");

        legalCaseContractMapper.setSearch(mpjQueryWrapper, searchValidate, new String[]{
                "like:caseName@t3.case_name:str",
                "=:status@t.status:str"
        });

        IPage<LegalCaseContractListedVo> iPage = legalCaseContractMapper.selectJoinPage(new Page<>(page, limit),
                LegalCaseContractListedVo.class, mpjQueryWrapper);
        for (LegalCaseContractListedVo vo : iPage.getRecords()) {
            if (vo.getCreateTime() != null) {
                String format = new SimpleDateFormat("yyyy-MM-dd mm:HH:ss").format(new Date(vo.getCreateTime() * 1000));
                vo.setCreateTimeStr(format);
            }
        }
        return PageResult.iPageHandle(iPage);
    }

    /**
     * 案件合同记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalCaseContract
     */
    @Override
    public LegalCaseContractDetailVo detail(Integer id) {
        LegalCaseContract model = legalCaseContractMapper.selectOne(
                new QueryWrapper<LegalCaseContract>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalCaseContractDetailVo vo = new LegalCaseContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 案件合同记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalCaseContractCreateValidate createValidate) {
        LegalCaseContract model = new LegalCaseContract();
        model.setAId(createValidate.getAId());
        model.setTemplateId(createValidate.getTemplateId());
        model.setCaseId(createValidate.getCaseId());
        model.setType(createValidate.getType());
        model.setUserId(createValidate.getUserId());
        model.setAreaMobile(createValidate.getAreaMobile());
        model.setUserMobile(createValidate.getUserMobile());
        model.setFileId(createValidate.getFileId());
        model.setFileJson(createValidate.getFileJson());
        model.setStatus(createValidate.getStatus());
        model.setStartDate(createValidate.getStartDate());
        model.setEndDate(createValidate.getEndDate());
        model.setSignFlowId(createValidate.getSignFlowId());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalCaseContractMapper.insert(model);
    }

    /**
     * 案件合同记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalCaseContractUpdateValidate updateValidate) {
        LegalCaseContract model = legalCaseContractMapper.selectOne(
                new QueryWrapper<LegalCaseContract>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setAId(updateValidate.getAId());
        model.setTemplateId(updateValidate.getTemplateId());
        model.setCaseId(updateValidate.getCaseId());
        model.setType(updateValidate.getType());
        model.setUserId(updateValidate.getUserId());
        model.setAreaMobile(updateValidate.getAreaMobile());
        model.setUserMobile(updateValidate.getUserMobile());
        model.setFileId(updateValidate.getFileId());
        model.setFileJson(updateValidate.getFileJson());
        model.setStatus(updateValidate.getStatus());
        model.setStartDate(updateValidate.getStartDate());
        model.setEndDate(updateValidate.getEndDate());
        model.setSignFlowId(updateValidate.getSignFlowId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        legalCaseContractMapper.updateById(model);
    }

    /**
     * 案件合同记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalCaseContract model = legalCaseContractMapper.selectOne(
                new QueryWrapper<LegalCaseContract>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalCaseContractMapper.delete(new QueryWrapper<LegalCaseContract>().eq("id", id));
    }

}
